import axios from 'axios'
import VueAxios from 'vue-axios'
import VueRouter from 'vue-router'
import Vue from 'vue'
import router from './router'

Vue.use(VueRouter)
Vue.use(VueAxios, axios)

const api = {
  signin: "/signin",
  signout: "/signout",
  signup: "/signup",
  whoami: "/whoami",
  users: "/api/v1/users"
}
global.api = api

axios.interceptors.response.use(
  resp => {
    return resp
  },
  error => {
    if (error.response) {
        switch (error.response.status) {
            case 401:
                router.replace({
                    path: '/signin',
                    query: {redirect: router.currentRoute.fullPath}
                })
        }
    }
    return Promise.reject(error.response.data)
  }
)